# -*- encoding: utf-8 -*-
'''
Filename         :8_myAtoi.py
Description      :
Time             :2022/08/05 08:45:40
Author           :daiyizheng
Email            :387942239@qq.com
Version          :1.0
'''

class Solution:
    def myAtoi(self, s: str) -> int:
        s = s.strip()
        if len(s)==0:
            return 0
        
        index = 0
        flag = 1
        if s[0]=="-":
            flag = -1
            index = index+1
        elif s[0]=="+":
            index+=1
        ans = 0
        while index < len(s):
            if not s[index].isdigit():
                break
            cur = int(s[index])
            # -2^32  xxxxxxx8
            if flag==-1 and (ans > 2**31//10 or(ans==2**31//10 and cur>8)):
                return -2**31
            elif flag==1 and (ans>2**31//10 or (ans==2**31//10 and cur>7)):
                return 2**31-1
            else:
                ans  = ans*10 +cur 
            index += 1
        
        return ans*flag
